﻿using DevExpress.XtraBars;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SMART_VIET_V01.Lib;

namespace SMART_VIET_V01
{
    public partial class frm_tracnghiem : Form
    {
        Cursor currentCursor;
        ColorPopup cp;
        List<string> ABCList = new List<string> { "A", "B", "C", "D", "E", "F", "G", "H", "I" };
        List<PictureBox> picList = new List<PictureBox>();
        List<RichTextBox> ricList = new List<RichTextBox>();
        DataTable dtCauhoi = new DataTable();
        Dictionary<int, DataTable> dicDapan = new Dictionary<int, DataTable>();
        //DataTable dtDapan = new DataTable();
        bool flagThemmoi = false;
        int cauhoiIndex = 0;
        public frm_tracnghiem()
        {
            InitializeComponent();
            CreateColorPopup(popupControlContainer1);
            for (int i = 8; i <= 50; i += 2)
                repositoryItemRichEditFontSizeEdit1.Items.Add(i);

            initDatatable();
            string rtfText = @"{\rtf1\ansi\ansicpg1252\deff0\deflang1031{\fonttbl{\f0\fnil\fcharset0 Arial;}}{\colortbl ;\red255\green0\blue0;}\viewkind4\uc1\pard\fs14 Sample\cf1\b\fs20 Entry\cf0\par}";
            string cl_cauhoi = (dtCauhoi.Rows.Count + 1).ToString();
            
            dtCauhoi.Rows.Add("Câu hỏi 1", rtfText);
            dtCauhoi.Rows.Add("Câu hỏi 2", rtfText);
            dtCauhoi.Rows.Add("Câu hỏi 3", rtfText);
            dtCauhoi.Rows.Add("Câu hỏi 4", rtfText);
            dtCauhoi.Rows.Add("Câu hỏi 5", rtfText);

            for(int i=0; i < dtCauhoi.Rows.Count; i++)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("cl_dapan", typeof(string));
                dt.Columns.Add("cl_noidung", typeof(string));

                dt.Rows.Add("0",rtfText);
                dt.Rows.Add("1", rtfText);
                dt.Rows.Add("0", rtfText);
                dt.Rows.Add("0", rtfText);

                dicDapan.Add(i,dt);
            }

            loadTreeView();
          
            enableButton(false);
            if (treeCauhoi.Nodes.Count == 0) barN.Visible = false;

            loadDapAn();
        }
        private void b_Click(object sender, EventArgs e)
        {
            foreach (var item in picList)
            {
                item.Image = null;
            }
            ((PictureBox)sender).Image = global::SMART_VIET_V01.Properties.Resources.accept;
            ((PictureBox)sender).SizeMode = PictureBoxSizeMode.CenterImage;

            //MessageBox.Show(((ListViewItem)((PictureBox)sender).Tag).Text);
        }
        #region init
        //Load Dapan
        public void loadDapAn()
        {

            for (int i = 0; i < ABCList.Count; i++)
            {
                string dapan = "0";
                string noidung = "";

                if (dicDapan.Keys.Count > 0 && dicDapan[i].Rows.Count <= (dicDapan[cauhoiIndex].Rows.Count+1))
                {
                    DataRow row = dicDapan[cauhoiIndex].Rows[i];
                    dapan = row[0].ToString();
                    noidung = row[1].ToString();
                }
                ListViewItem item = new ListViewItem();
                ListViewItem.ListViewSubItem sub_item1 = new ListViewItem.ListViewSubItem();
                ListViewItem.ListViewSubItem sub_item2 = new ListViewItem.ListViewSubItem();
                sub_item1.Text = dapan;
                sub_item2.Text = noidung;
                item.SubItems.AddRange(new ListViewItem.ListViewSubItem[] { sub_item1, sub_item2 });
                listViewEx1.Items.Add(item);

                GroupBox gr_s = new GroupBox();
                Label lb = new Label();
                lb.Text = ABCList[i];
                lb.Dock = DockStyle.Fill;
                lb.TextAlign = ContentAlignment.MiddleCenter;
                gr_s.Controls.Add(lb);


                GroupBox gr_a = new GroupBox();

                PictureBox pc = new PictureBox();
                pc.Name = "pic_" + i;
                pc.Tag = item;
                item.Tag = pc;
                if (dapan.Equals("1")) { 
                pc.Image = global::SMART_VIET_V01.Properties.Resources.accept;
                pc.SizeMode = PictureBoxSizeMode.CenterImage;
                }
                pc.Click += new EventHandler(b_Click);
                pc.Dock = DockStyle.Fill;
                picList.Add(pc);
                gr_a.Controls.Add(pc);


                GroupBox gr = new GroupBox();

                item.Text = ABCList[i];
                RichTextBox ric = new RichTextBox();
                ric.Name = "ric_" + i;
                ric.Rtf = noidung;
                ric.Tag = item;
                ric.BorderStyle = BorderStyle.None;
                ric.Dock = DockStyle.Fill;
                gr.Controls.Add(ric);


                listViewEx1.AddEmbeddedControl(gr_s, 0, item.Index);
                listViewEx1.AddEmbeddedControl(gr_a, 1, item.Index);
                listViewEx1.AddEmbeddedControl(gr, 2, item.Index);
            }
        }


        private FontStyle rtPadFontStyle()
        {
            FontStyle fs = new FontStyle();
            if (iBold.Down)
                fs |= FontStyle.Bold;
            if (iItalic.Down)
                fs |= FontStyle.Italic;
            if (iUnderline.Down)
                fs |= FontStyle.Underline;
            return fs;
        }
        protected void InitFormat()
        {
            iBold.Down = SelectFont.Bold;
            iItalic.Down = SelectFont.Italic;
            iUnderline.Down = SelectFont.Underline;
            iProtected.Down = rtPad.SelectionProtected;
            iBullets.Down = rtPad.SelectionBullet;
            switch (rtPad.SelectionAlignment)
            {
                case HorizontalAlignment.Left:
                    iAlignLeft.Down = true;
                    break;
                case HorizontalAlignment.Center:
                    iCenter.Down = true;
                    break;
                case HorizontalAlignment.Right:
                    iAlignRight.Down = true;
                    break;
            }
        }
        protected void InitPaste()
        {
            iPaste.Enabled = rtPad.CanPaste(DataFormats.GetFormat(0));
        }
        protected Font SelectFont
        {
            get
            {
                if (rtPad.SelectionFont != null)
                    return rtPad.SelectionFont;
                return rtPad.Font;
            }
        }
        protected void RefreshForm(bool b)
        {
            if (b)
            {
                currentCursor = Cursor.Current;
                Cursor.Current = Cursors.WaitCursor;
                Refresh();
            }
            else
            {
                Cursor.Current = currentCursor;
            }
        }

        private void CreateColorPopup(PopupControlContainer container)
        {
            cp = new ColorPopup(container, iFontColor, rtPad);
        }

        private void initDatatable()
        {
            dtCauhoi = new DataTable();
            dtCauhoi.Columns.Add("cl_cauhoi", typeof(string));
            dtCauhoi.Columns.Add("cl_noidung", typeof(string));
        }

        private void loadTreeView()
        {
            treeCauhoi.Nodes.Clear();
            foreach (DataRow item in dtCauhoi.Rows)
            {
                TreeNode node = new TreeNode();
                node.Text = item["cl_cauhoi"].ToString();
                treeCauhoi.Nodes.Add(node);
            }

        }
        #endregion
        private void iFontStyle_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            rtPad.SelectionFont = new Font(SelectFont.FontFamily.Name, SelectFont.Size, rtPadFontStyle());
        }
        private void iCut_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            rtPad.Cut();
            InitPaste();
        }

        private void iCopy_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            rtPad.Copy();
            InitPaste();
        }

        private void iPaste_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            rtPad.Paste();
        }

        private void iUndo_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            rtPad.Undo();
            iUndo.Enabled = rtPad.CanUndo;
            InitFormat();
        }
        private void iAlign_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (iAlignLeft.Down)
                rtPad.SelectionAlignment = HorizontalAlignment.Left;
            if (iCenter.Down)
                rtPad.SelectionAlignment = HorizontalAlignment.Center;
            if (iAlignRight.Down)
                rtPad.SelectionAlignment = HorizontalAlignment.Right;
        }

        private void iBullets_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            rtPad.SelectionBullet = iBullets.Down;
        }
        private void iFont_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            FontDialog dlg = new FontDialog();
            dlg.Font = (Font)SelectFont.Clone();
            dlg.ShowColor = true;
            dlg.Color = rtPad.SelectionColor;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                rtPad.SelectionFont = (Font)dlg.Font.Clone();
                rtPad.SelectionColor = dlg.Color;
            }
        }

        private void iFontColor_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            rtPad.SelectionColor = cp.ResultColor;
        }

        private void fontSize_ItemPress(object sender, ItemClickEventArgs e)
        {
            rtPad.SelectionFont = new Font(rtPad.SelectionFont.FontFamily, (float)Convert.ToDouble(fontSize.EditValue), rtPad.SelectionFont.Style);
        }
        private void fontSize_EditValueChanged(object sender, EventArgs e)
        {
            rtPad.SelectionFont = new Font(rtPad.SelectionFont.FontFamily, (float)Convert.ToDouble(fontSize.EditValue), rtPad.SelectionFont.Style);
        }

        private void iSave_ItemClick(object sender, ItemClickEventArgs e)
        {
            string rtfText = rtPad.Rtf;
            if (flagThemmoi)
            {
                string cl_cauhoi = (dtCauhoi.Rows.Count + 1).ToString();
                dtCauhoi.Rows.Add("Câu hỏi " + cl_cauhoi, rtfText);
            }
            else
            {
                dtCauhoi.Rows[cauhoiIndex]["cl_noidung"] = rtPad.Rtf;
            }
            loadTreeView();
            flagThemmoi = false;
            rtPad.Clear();
            enableButton(false);
        }

        private void btThemHinh_ItemClick(object sender, ItemClickEventArgs e)
        {

        }

        private void btChupHinh_ItemClick(object sender, ItemClickEventArgs e)
        {

        }

        private void btXoa_ItemClick(object sender, ItemClickEventArgs e)
        {

        }

        private void btThem_ItemClick(object sender, ItemClickEventArgs e)
        {
            flagThemmoi = true;
            enableButton(true);
            rtPad.Clear();
        }

        private void bar_tat_ItemClick(object sender, ItemClickEventArgs e)
        {

        }

        private void bar_Chen_ItemClick(object sender, ItemClickEventArgs e)
        {

        }

        private void treeCauhoi_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (treeCauhoi.SelectedNode != null)
            {
                cauhoiIndex = treeCauhoi.SelectedNode.Index;
                DataRow dr = dtCauhoi.Rows[cauhoiIndex];
                rtPad.Rtf = dr["cl_noidung"].ToString();
                enableButton(false);
            }
        }

        private void iEdit_ItemClick(object sender, ItemClickEventArgs e)
        {
            enableButton(true);
        }
        private void enableButton(bool flag)
        {
            if (flag)
            {
                barN.Visible = false;
            }
            else
            {
                if (treeCauhoi.SelectedNode != null)
                { barN.Visible = true; }
            }
            barS.Visible = flag;
            barE.Visible = flag;
            // treeDapan.Enabled = flag;
        }

        private void listViewEx1_MouseClick(object sender, MouseEventArgs e)
        {
            ListViewItem i = listViewEx1.GetItemAt(e.X, e.Y);
            //ListViewItem.ListViewSubItem s = i.GetSubItemAt(e.X,e.Y);
            MessageBox.Show(i.Index + "");
        }
    }
}
